package com.rabbiter.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.rabbiter.entity.Goods;
import com.rabbiter.mapper.GoodsMapper;
import com.rabbiter.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.util.MinioUtil;
import com.rabbiter.util.RedisUtil;
import com.rabbiter.vo.GoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private RedisUtil redisUtil;

private final String GOODS_CACHE = "goods:name:";

    @Override
    public IPage pageCC(IPage<Goods> page, Wrapper wrapper) {
        return goodsMapper.pageCC(page,wrapper);
    }

    @Override
    public void saveAll(Goods goods) {
        //先查询数据库里有没有这个商品(名称，商品类型，存储仓库这三个来确定是否存在) 如果有则更新 没有则插入 要在对应的仓库和类型
        Goods searchDB = new Goods();
        searchDB.setName(goods.getName());
        searchDB.setGoodstype(goods.getGoodstype());
        searchDB.setStorage(goods.getStorage());
        searchDB.setPrice(goods.getPrice());
        Goods searchGoods = goodsMapper.selectByNameAndTypeAndStorage(searchDB);
        if (searchGoods==null){
            //如果数据库里没有这个商品则插入
            Goods insertGoods = new Goods();
            BeanUtils.copyProperties(goods,insertGoods);
            //如果没有图片设置默认图片
            if (!StringUtils.isNotBlank(insertGoods.getImage())) {
                insertGoods.setImage("http://116.196.94.204:9000/warehouse/OIP-C.jpg");
            }
            goodsMapper.insert(goods);
            //将商品加入缓存
            redisUtil.set(GOODS_CACHE+insertGoods.getName(),insertGoods);
        }else{
            //如果数据库里存在这个商品则更新
            Integer count = searchGoods.getCount();
            searchGoods.setCount(count+goods.getCount());
            goodsMapper.updateById(searchGoods);
            //更新缓存
            redisUtil.set(GOODS_CACHE+goods.getName(),searchGoods);
        }
    }

    @Override
    public String updateImage(Integer id, MultipartFile image) {
        try {
            Goods goods = new Goods();
            String URL = minioUtil.uploadFile(image);
            goods.setId(id);
            goods.setImage(URL);
            goodsMapper.updateById(goods);
            return URL;
        } catch (Exception e) {
            log.warn(e.getMessage());
            return null;
        }
    }

    @Override
    public List<GoodsVO> getGoodsByTypeId(Integer id) {
        List<Goods> goodsList = goodsMapper.getGoodsByTypeId(id);
        if(goodsList==null|| goodsList.isEmpty()){
            return Collections.emptyList();
        }
        List<GoodsVO> goodsVOS =new ArrayList<>();
        goodsList.forEach(goods -> {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(goods,goodsVO);
            goodsVOS.add(goodsVO);
        });

        return goodsVOS;
    }
}
